#include "MagnetBuff.h"
#include "MainLayer.h"
#include "Item.h"


MagnetBuff::MagnetBuff(void)
{
	_effect = NULL;
	_stateBar = NULL;
	_listWave = NULL;
	_currentTime = TIME_DELAY;
}


MagnetBuff::~MagnetBuff(void)
{
	CC_SAFE_RELEASE(_effect);
	CC_SAFE_RELEASE(_stateBar);
	CC_SAFE_RELEASE(_listWave);
}

bool MagnetBuff::init()
{
	if (!BaseBuff::init())
		return false;

	CCSprite* sprite = CCSprite::createWithSpriteFrameName("magnet_icon.png");
	sprite->setAnchorPoint(ccp(0.5f, 0.5f));
	sprite->setPosition(ccp(getBlackMask()->boundingBox().getMidX(), getBlackMask()->boundingBox().getMidY()));
	addChild(sprite, 0);

	getBlackMask()->setScaleY(1.0f);
	setWaves(CCArray::create());

	return true;
}

void MagnetBuff::onStart( StateBar* stateBar )
{
	setStateBar(stateBar);
	schedule(SEL_SCHEDULE(&MagnetBuff::processing));
}

void MagnetBuff::onComplete()
{
	PlayerPlane* plane = getPlane();
	CCObject* it;
	CCARRAY_FOREACH(_listWave, it)
	{
		plane->removeChild(dynamic_cast<CCSprite*>(it));
	}
	unschedule(SEL_SCHEDULE(&MagnetBuff::processing));
}

void MagnetBuff::processing( float dt )
{
	CCArray* listItem = _stateBar->getMainLayer()->getItems();
	PlayerPlane* plane = _stateBar->getMainLayer()->getMyPlant();
	CCPoint planePoint = ccp(plane->boundingBox().getMidX(), plane->boundingBox().getMidY());
	
	CCObject* it;
	int fore = 7;
	int fore2 = 10;
	CCARRAY_FOREACH(listItem, it)
	{
		Item* item = dynamic_cast<Item*>(it);
		CCPoint itemPoint = ccp(item->boundingBox().getMidX(), item->boundingBox().getMidY());
		if (itemPoint.y - planePoint.y < EFFECT_DISTANCE)
		{
			CCPoint a =  (planePoint - itemPoint);
			a.x = fore * (a.x + fore2);
			a.y = fore * (a.y + fore2);
			item->setVelocity(a);
		} else {
			if (item->getMagnetTime() > MAGNET_TIME)
			{
				CCPoint a =  (planePoint - itemPoint);
				a.x = fore * (a.x + fore2);
				a.y = fore * (a.y + fore2);
				item->setVelocity(a);
			}
		}
	}

	// Effect
	float timeDelay = TIME_DELAY;
	float timeFadeIn = TIME_FADEOUT;
	float defaultScale = DEFAULT_SCALE;
	ccColor3B color = WAVE_COLOR;
	int maxRing = MAX_WAVE;
	if (_currentTime > timeDelay)
	{
		if (_listWave->count() < maxRing)
		{
			CCSprite* ring = CCSprite::create("wsparticle_19.png");
			ring->setPosition(ccp(plane->boundingBox().size.width/2, plane->boundingBox().size.height/2));
			ring ->setColor(color);
			ring->setScale(defaultScale);
			ring->setOpacity(0);
			plane->addChild(ring);
			_listWave->addObject(ring);
		} else  {
			CCSprite* firstRing = dynamic_cast<CCSprite*>(_listWave->objectAtIndex(0));
			firstRing->setScale(defaultScale);
			_listWave->removeObjectAtIndex(0);
			_listWave->addObject(firstRing);
			firstRing->setOpacity(0);
		}
		_currentTime = 0;
	}
	if (_currentTime > timeDelay - timeFadeIn && _listWave->count() != 0)
	{
		CCSprite* firstRing = dynamic_cast<CCSprite*>(_listWave->objectAtIndex(_listWave->count() - 1));
		float a = 255 * (1 - (timeDelay - _currentTime) / timeFadeIn);
		firstRing->setOpacity(a);
	}

	for (int i = 0; i < _listWave->count(); i++)
	{
		CCSprite* ring = dynamic_cast<CCSprite*>(_listWave->objectAtIndex(i));
		float a =  (dt / timeDelay) * (defaultScale / maxRing);
		ring->setScale(ring->getScale() - a);

		a = 1;
	}

	_currentTime += dt;
}

